enum judgetype
{
  circle=0,
  rectangle=1,
  poly=4,
  undefi=5,
}
abstract class CCOLLISION{

	abstract get type():judgetype;  //碰撞盒类型
	abstract set shapeCollisionSwitch(a:boolean) //开启或关闭形状显示

	abstract NormalVector():any;  //得到法向量
	abstract get relpoint():any; //得到舞台坐标
	abstract get r():number; //得到圆形半径
    abstract setshape(x1y1:any,x2y2:any,x3y3:any,x4y4?:any,x5y5?:any,color?:any)//多边形输入3-5个egret.point，圆形输入x y r 矩形输入x y w h
    public parent:CBASE //指针绑定
	public shapepoint:egret.Point[]=[]
	public shape:egret.Shape
	public judgetype:judgetype
	public  name:string         //每个碰撞区设定名字
	public static number=0      //自动命名
	public judge:boolean=false        //碰撞检测时间
	public judgeswitch:boolean=false  //是否碰撞
  
	protected Pointrevolve(POINT:egret.Point,o:egret.Point,angle:number):egret.Point //一点绕任意一点旋转任意度数
	{
		 
		 var point =new egret.Point
		 point.x=(POINT.x-o.x)*Math.cos(angle*Math.PI / 180)*this.parent.scaleX-(POINT.y-o.y)*Math.sin(angle*Math.PI / 180)*this.parent.scaleY+o.x
		 point.y=(POINT.x-o.x)*Math.sin(angle*Math.PI/180)*this.parent.scaleX+(POINT.y-o.y)*Math.cos(angle*Math.PI / 180)*this.parent.scaleY+o.y
		
		 return point
	}
	public constructor(ptr:CBASE) {
		this.parent=ptr
		CCOLLISION.number++;
		this.name=CCOLLISION.number.toString();
	}
	public collision_twoshape(obj:CCOLLISION)
	{
      let anormal=this.NormalVector()
	  let bnormal=obj.NormalVector()
	  let apoint=this.relpoint
	  let bpoint=obj.relpoint
      //console.log(anormal,bnormal)
	  
		   for(var a=0;a<anormal.length;a++) //先判定碰撞方的轴
		   {
			  let amax:number=-9999 //碰撞方最大
		      let alim:number=9999  //碰撞方最小
		      let bmax:number=-9999 //被碰撞方最大
		      let blim:number=9999  //被碰撞方最小
                for(var b=0;b<apoint.length;b++)
				{
					//所有顶点与原点连线做向量和单位向量轴点乘
					let resultlong=apoint[b].x*anormal[a].x+apoint[b].y*anormal[a].y
                   if(resultlong>amax)
				   {
                      amax=resultlong
				   }
				   if(resultlong<alim)
				   {
                      alim=resultlong
				   }
				}
				 for(b=0;b<bpoint.length;b++)
				{
					
					let resultlong=bpoint[b].x*anormal[a].x+bpoint[b].y*anormal[a].y
					if(resultlong>bmax)
				   {
                      bmax=resultlong
				   }
				   if(resultlong<blim)
				   {
                      blim=resultlong
				   }
				}
				if(!(amax>=blim&&bmax>=alim))
				{
					this.judge=false;
				    obj.judge=false;
                    return false //比较大小，如果未碰撞则返回
				}
		   }
		   for(var a=0;a<bnormal.length;a++) //判断被碰撞方轴
		   {
			  let amax:number=-9999 //碰撞方最大
		      let alim:number=9999  //碰撞方最小
		      let bmax:number=-9999 //被碰撞方最大
		      let blim:number=9999  //被碰撞方最小
                for(var b=0;b<bpoint.length;b++)
				{
				   let resultlong=bpoint[b].x*bnormal[a].x+bpoint[b].y*bnormal[a].y
                   if(resultlong>bmax)
				   {
                      bmax=resultlong
				   }
				   if(resultlong<blim)
				   {
                      blim=resultlong
				   }
				}
				 for(b=0;b<apoint.length;b++)
				{
                  
				   let resultlong=apoint[b].x*bnormal[a].x+apoint[b].y*bnormal[a].y
					if(resultlong>amax)
				   {
                      amax=resultlong
				   }
				   if(resultlong<alim)
				   {
                      alim=resultlong
				   }
				}
					if(!(amax>=blim&&bmax>=alim))
				   {
					   this.judge=false;
				        obj.judge=false;
                        return false //比较大小，如果未碰撞则返回
				    }
		   }
		    this.judge=true;
			obj.judge=true;
		   return true//所有结果都未return
	}
	public collision_onecircle(obj:CCOLLISION)
	{
		var pointa=this.relpoint
		var pointb=obj.relpoint
		var nomalpoint:egret.Point
		var long=-999
		if(this.type==judgetype.circle)
		{
				
           for(let a=0;a<pointb.length;a++)
		   {
			  let c=pointb[a].subtract(pointa)
			  if((c.x*c.x+c.y*c.y)>long)
			  {
				  long=(c.x*c.x+c.y*c.y)
				  nomalpoint=c
			  }
		   }
		   let nomal=Math.sqrt(nomalpoint.x*nomalpoint.x+nomalpoint.y+nomalpoint.y)
		   nomalpoint.x/=nomal
		   nomalpoint.y/=nomal
		   let amax:number=-9999 //碰撞方最大
		   let alim:number=9999  //碰撞方最小
		   let bmax:number=-9999 //被碰撞方最大
		   let blim:number=9999  //被碰撞方最小
		    for(var b=0;b<pointb.length;b++)
				{
					//所有顶点与原点连线做向量和单位向量轴点乘
				   let resultlong=pointb[b].x*nomalpoint.x+pointb[b].y*nomalpoint.y
                   if(resultlong>amax)
				   {
                      amax=resultlong
				   }
				   if(resultlong<alim)
				   {
                      alim=resultlong
				   }
				}
			  bmax=pointa.x*nomalpoint.x+pointa.y*nomalpoint.y+this.r
			  blim=bmax-2*this.r
			   if(!(amax>=blim&&bmax>=alim))
			   {
				   this.judge=false;
				   obj.judge=false;
			   }
				     return false //比较大小，如果未碰撞则返回
		}
		else
		{	
           for(let a=0;a<pointa.length;a++)
		   {
			  let c=pointa[a].subtract(pointb)
			  if((c.x*c.x+c.y*c.y)>long)
			  {
				  long=(c.x*c.x+c.y*c.y)
				   nomalpoint=c
			  }
		   }
		    let nomal=Math.sqrt(nomalpoint.x*nomalpoint.x+nomalpoint.y+nomalpoint.y)
		   nomalpoint.x/=nomal
		   nomalpoint.y/=nomal
		   let amax:number=-9999 //碰撞方最大
		   let alim:number=9999  //碰撞方最小
		   let bmax:number=-9999 //被碰撞方最大
		   let blim:number=9999  //被碰撞方最小
		   for(var b=0;b<pointa.length;b++)
				{
					//所有顶点与原点连线做向量和单位向量轴点乘
				   let resultlong=pointa[b].x*nomalpoint.x+pointa[b].y*nomalpoint.y
                   if(resultlong>amax)
				   {
                      amax=resultlong
				   }
				   if(resultlong<alim)
				   {
                      alim=resultlong
				   }
				}
			  bmax=pointb.x*nomalpoint.x+pointb.y*nomalpoint.y+obj.r
			  blim=bmax-2*obj.r
			   if(!(amax>=blim&&bmax>=alim))
			   {
				   this.judge=false;
				   obj.judge=false;
				   return false //比较大小，如果未碰撞则返回
			   }
				    
		}
		this.judge=true;
		obj.judge=true;
	   return true;
		
	}
	public collision_twocircle(obj:CCOLLISION)
	{
		let apoint=this.relpoint
	    let bpoint=obj.relpoint
        let cpoint= apoint.subtract(bpoint)
		if(Math.sqrt(cpoint.x*cpoint.x+cpoint.y*cpoint.y)<=(this.r+obj.r))
		{
			this.judge=true
			obj.judge=true
			return true;
		}
		else
		{
			this.judge=false
			obj.judge=false
			return false;
		}
	}


}